package controler;

import logs.LogLevelEnum;
import logs.LogTypesEnum;
import modele.interfaces.SimulationAbstract;
import modele.interfaces.SimulationInterface;
import controler.interfaces.ControlerAbstract;
import controler.interfaces.ControlerInterface;
import controler.interfaces.ModelControlerInterface;
import controler.interfaces.StepReportInterface;
import controler.interfaces.ViewControleurInterface;

public class ModelControler extends ControlerAbstract implements ModelControlerInterface {

	private ViewControleurInterface viewControler;
	Thread thd;
	SimulationInterface actualSimulation;

	public ModelControler() {
		setName();
	}

	@Override
	public void setName() {
		setName("Model_Controler");
	}

	@Override
	public void launch(SimulationInterface simul) {
		if (simul != null) {
			actualSimulation = simul;
			simul.init(this);
			viewControler.initwithStaticData(simul.getParameters());
			viewControler.initWithDynamicData(simul.getDynamicData());

			logSimulationPassedToBeLaunched(simul);
			simul.okToRun();
			thd = new Thread(simul);
			thd.start();
		}
	}

	private void logSimulationPassedToBeLaunched(SimulationInterface simul) {
		String log = " (" + this.getName() + ") " + simul.getName() + " passed to be launched";
		super.log(LogLevelEnum.INFO, LogTypesEnum.Simul, log);
	}

	@Override
	public void initView(ViewControleurInterface viewControler) {
		this.viewControler = viewControler;
	}

	@Override
	public void aknowledgeEndOfStep(StepReportInterface stepReport) {
		viewControler.updateAfterStep(stepReport);
	}

	@Override
	public void quit() {
		if (thd.isAlive()) {
			thd.interrupt();
		}

	}

	@Override
	public void triggerAStep() {
		actualSimulation.triggerAStep();
	}

	@Override
	public void switchStepByStep() {
		actualSimulation.switchStepByStep();

	}
}
